package com.springboot.demo.design.pattern.strategy;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;

/**
 * 会员上下文
 *
 * @author supu
 * @date 2019-07-05 14:46
 **/
@Service
public class Price {
    private Map<String, MemberStrategy> memberStrategyMap = new ConcurrentHashMap<>(16);

    public Price(List<MemberStrategy> memberStrategies) {
        memberStrategies.forEach(strategy -> {
            memberStrategyMap.put(strategy.getType(), strategy);
        });
    }

    public double quotePrice(String type, double price) {
        MemberStrategy memberStrategy = memberStrategyMap.get(type);
        if (memberStrategy == null) {
            return price;
        }
        return memberStrategy.calculate(price);
    }
}
